package p2pscrabble;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class PlayerScorePanel extends JPanel {
	JLabel namePlayer1;
	JLabel namePlayer2;
	JLabel scorePlayer1;
	JLabel scorePlayer2;
	JLabel turn;
	JLabel lastTurn1Label;
	JLabel lastTurn2Label;
	
	int score1;
	int score2;

	public PlayerScorePanel(String player1, String player2, boolean myTurn,
			boolean isHost) {
		score1 = 0;
		score2 = 0;

		// displaying name
		namePlayer1 = isHost ? new JLabel(player1 + " (me | host)")
				: new JLabel(player1);
		namePlayer2 = isHost ? new JLabel(player2) : new JLabel(player2
				+ " (me)");

		scorePlayer1 = new JLabel(Integer.toString(score1));
		scorePlayer2 = new JLabel(Integer.toString(score2));
		turn = new JLabel(">>");
		lastTurn1Label = new JLabel("(0)");
		lastTurn2Label = new JLabel("(0)");

		this.add(namePlayer1);
		this.add(namePlayer2);
		this.add(scorePlayer1);
		this.add(scorePlayer2);
		this.add(turn);
		this.add(lastTurn1Label);
		this.add(lastTurn2Label);

		if ((myTurn && isHost) || (!myTurn && !isHost))
			turn.setBounds(5, 10, 20, 50);
		else
			turn.setBounds(5, 40, 20, 50);

		namePlayer1.setBounds(30, 10, 100, 50);
		namePlayer2.setBounds(30, 40, 100, 50);
		scorePlayer1.setBounds(200, 10, 100, 50);
		scorePlayer2.setBounds(200, 40, 100, 50);
		lastTurn1Label.setBounds(240, 10, 50, 50);
		lastTurn2Label.setBounds(240, 40, 50, 50);
	}

	public void addScore(int player, int score) {
		if (player == 0) {
			score1 += score;
			lastTurn1Label.setText("(" + score + ")");
			lastTurn2Label.setText("(0)");
		} else {
			score2 += score;
			lastTurn1Label.setText("(0)");
			lastTurn2Label.setText("(" + score + ")");
		}
		updateDisplay();
	}

	public void switchTurn() {
		int y = turn.getY();
		y = (y + 30) % 60;
		turn.setBounds(5, y, 20, 50);
		repaint();
	}

	public int getScore(int player) {
		if (player == 0)
			return Integer.parseInt(scorePlayer1.getText());
		return Integer.parseInt(scorePlayer2.getText());
	}
	
	public void setTurn(int player) {
		turn.setBounds(5, 10 + player * 30, 20, 50);
	}

	public void setScore(int player1Score, int player2Score) {
		score1 = player1Score;
		score2 = player2Score;

		lastTurn1Label.setText("(0)");
		lastTurn2Label.setText("(0)");
		
		updateDisplay();
	}

	public void updateDisplay() {
		scorePlayer1.setText(Integer.toString(score1));
		scorePlayer2.setText(Integer.toString(score2));
	}
}